const connections = require('../app/database');
class UserService {

    // CREATE TABLE IF NOT EXISTS `users` (
    //     id INT PRIMARY KEY AUTO_INCREMENT,
    //     name VARCHAR(20) NOT NULL UNIQUE,
    //     password VARCHAR(50) NOT NULL,
    //     createAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    //     updateAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    // );
    
    async create(user) {
        const { name, password } = user;
        const statement = `INSERT INTO users (name, password) VALUES (?, ?);`;
        const result = await connections.execute(statement, [name, password]);

        return result[0];
    }

    async getUserByName(name) {
        const statement = `SELECT * FROM users WHERE name = ?;`;
        const result = await connections.execute(statement, [name]);

        return result[0];
    }

    async updateAvatarUrlById(avatarUrl, userId) {
        const statement = `
            UPDATE users SET avatar_url = ? WHERE id = ?;
        `;
        const [result] = await connections.execute(statement, [avatarUrl, userId]);

        return result;
    }
}

module.exports = new UserService();